﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LevelUp
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void cmdSubmit_Click(object sender, EventArgs e)
        {
            lblErr.Text = "";
            lblErr.Visible = false;

            if (txtComments.Text != "" && txtName.Text != "")
            {
                if (String.IsNullOrEmpty(txtEmail.Text) && String.IsNullOrEmpty(txtPhone.Text))
                {
                    lblErr.Text = "Please provide atleast one method of contact";
                    lblErr.Visible = true;
                }
                else
                {
                    try
                    {
                        var fromAddress = new MailAddress("mvron7@gmail.com", "Website Admin");
                        var toAddress = new MailAddress("kelley@levelupparties.com", "LevelUp");
                        const string fromPassword = "muralichinnu21";
                        const string subject = "New inquiry from website - levelupparties.com";
                        string body = "";
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("Name: " + txtName.Text);
                        sb.AppendLine("Phone: " + txtPhone.Text);
                        sb.AppendLine("Email: " + txtEmail.Text);
                        sb.AppendLine("Preferred Contact Method: " + cmdMethod.SelectedValue);
                        sb.AppendLine("Message:");
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine(txtComments.Text);

                        body = sb.ToString();

                        var smtp = new SmtpClient
                        {
                            Host = "smtp.gmail.com",
                            Port = 587,
                            EnableSsl = true,
                            DeliveryMethod = SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                        };
                        using (var message = new MailMessage(fromAddress, toAddress)
                        {
                            Subject = subject,
                            Body = body
                        })
                        {
                            smtp.Send(message);
                        }

                        lblErr.Text = "Your message was sent to kelley@levelupparties.com";
                        lblErr.Visible = true;
                        txtName.Text = "";
                        txtComments.Text = "";
                        cmdMethod.SelectedIndex = 0;
                        txtEmail.Text = "";
                        txtPhone.Text = "";
                    }
                    catch (Exception ex)
                    {
                        lblErr.Text = "There was a problem sending your message. Please try again later";
                        lblErr.Text = ex.Message;
                        lblErr.Visible = true;
                    }
                }
            }
            else
            {
                lblErr.Text = "Name and Message are required to send an email";
                lblErr.Visible = true;
            }

        }
    }
}